package com.kingdee.pda.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.example.basecommon.utils.PreferencesUtils;
import com.kingdee.pda.MyApplication;
import com.kingdee.pda.entity.LanguageEntity;

import java.util.Locale;

/**
 * Created by zhanlg
 * DATE 2019/1/8
 * TIME 9:44
 */

public class LanguageUtil {


    public static String getCurrentLocaleText() {
        String locale = PreferencesUtils.getString(MyApplication.getInstance().getApplicationContext(), ConstantPreference.SPF_KEY_SELECTED_LOCALE);
        if (TextUtils.isEmpty(locale)) {
            return "中文简体";
        }
        return locale;
    }

    public static LanguageEntity getSelectedLocale(String localeText) {
        Locale locale;
        int localeId;
        if (TextUtils.isEmpty(localeText)) {
            locale = Locale.SIMPLIFIED_CHINESE;
            localeId = 2052;
        } else {
            switch (localeText) {
                case "中文繁體":
                    locale = Locale.TRADITIONAL_CHINESE;
                    localeId = 3076;
                    break;
                case "English":
                    locale = Locale.ENGLISH;
                    localeId = 1033;
                    break;
                case "中文简体":
                default:
                    locale = Locale.SIMPLIFIED_CHINESE;
                    localeId = 2052;
                    break;
            }
        }

        return new LanguageEntity(locale, localeId);
    }

    public static int getSelectedId(String localeText) {
        return getSelectedLocale(localeText).getLocaleId();
    }

    public static LocaleList getLocaleList() {
        //由于API仅支持7.0，需要判断，否则程序会crash
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = LocaleList.getDefault();
        }
        return null;
    }

    @SuppressWarnings("deprecation")
    public static void setLocale(Context context) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0以上
            Locale currentLocale = getCurrentLocale(context);
            conf.setLocale(currentLocale);
            LocaleList localeList = new LocaleList(currentLocale);
            LocaleList.setDefault(localeList);
            conf.setLocales(localeList);
        } else {
            conf.locale = getCurrentLocale(context);
        }
        res.updateConfiguration(conf, dm);
    }

    /**
     * 获取当前语言
     *
     * @return
     */
    public static Locale getCurrentLocale(Context context) {
        return getSelectedLocale(PreferencesUtils.getString(context, ConstantPreference.SPF_KEY_SELECTED_LOCALE)).getLocale();
    }


    public static Context attachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context);
        } else {
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context) {
        Resources resources = context.getResources();
        Locale locale = getCurrentLocale(context);

        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }


}
